package com.cenluan.common;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.aliyun.openservices.oss.OSSClient;
import com.cenluan.IndexController;
import com.cenluan.beetl.BeetlRenderFactory;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;

public class Config extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("env.properties");
		// 开发模式
		if (getPropertyToBoolean("devMode")) {
			me.setDevMode(true);
		}
		// 错误页面使用jfinal默认的，不再设置
		// beetl
		me.setMainRenderFactory(new BeetlRenderFactory());
		// beetl设置全局变量
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("ctx", JFinal.me().getContextPath());
		BeetlRenderFactory.GT.setSharedVars(model);

		// 设置下载文件大小和存放位置
		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath()
				+ File.separator + "upload");
		// oss
		OssManager.setClient(new OSSClient(getProperty("oss.accessKeyId"),
				getProperty("oss.accessKeySecret")));
		OssManager.setServer(getProperty("oss.server"));
		OssManager.setImageBucket(getProperty("oss.imageBucket"));
		OssManager.setImageDomain(getProperty("oss.imageDomain"));
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		// ehcache
		me.add(new EhCachePlugin(PathKit.getWebRootPath() + File.separator
				+ "WEB-INF" + File.separator + "ehcache.xml"));
		// druid
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("mysql.url"),
				getProperty("mysql.username"), getProperty("mysql.password"),
				getProperty("mysql.driverClass"));
		druidPlugin
				.setMaxPoolPreparedStatementPerConnectionSize(getPropertyToInt("druid.maxPoolPreparedStatementPerConnectionSize"));
		me.add(druidPlugin);
		druidPlugin.setInitialSize(getPropertyToInt("druid.initialSize"));
		druidPlugin.setMinIdle(getPropertyToInt("druid.minIdle"));
		druidPlugin.setMaxActive(getPropertyToInt("druid.maxActive"));
		ActiveRecordPlugin mysqlArp = new ActiveRecordPlugin(druidPlugin);
		mysqlArp.setDialect(new MysqlDialect());
		me.add(mysqlArp);

	}

	@Override
	public void configInterceptor(Interceptors me) {

	}

	@Override
	public void configHandler(Handlers me) {

	}

}
